//使用机构活动模板
exports.editOrganizationDiscountActivity = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    //任务流创建
    async.waterfall([
            //机构模板活动
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationActivity + "/oa_id/" + restfulParams.oa_id + "/oa_type/5",
                        method: 'GET',
                    },
                    function (err, data) {
                        if (err || data.data == '') {
                            return RES.response(res, false, data);
                        }

                        var duration = data.data[0].oa_class_duration;
                        var duration_split = duration.split("|");
                        console.log(duration_split);
                        resultData.activity = data.data;
                        resultData.activity[0].course_count = duration_split[0];
                        resultData.activity[0].course_time = duration_split[1];
                        cb();
                    }
                )
            },
        function (cb) {
            if (resultData.activity[0].oa_skin_id > 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchActivitySkin + "/as_id/" + resultData.activity[0].oa_skin_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        if (err) {

                            return cb(err, data);
                        }
                        resultData.skinData = data.data;
                        cb();
                    }
                )

            } else {
                resultData.skinData = [];
                cb()
            }
        },
        function (cb) {
            if (resultData.activity[0].oa_music_id > 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchActivityMusic + "/am_id/" + resultData.activity[0].oa_music_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        if (err) {

                            return cb(err, data);
                        }

                        resultData.musicData = data.data;
                        cb();
                    }
                )

            } else {
                resultData.musicData = [];
                cb()
            }
        },
            //机构模板活动拓展
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationDiscountsActivityConfig + "/dac_oaid/" + restfulParams.oa_id,
                        method: 'GET',

                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err || data.data == '') {
                            return RES.response(res, false, data);
                        }
                        resultData.config = data.data;
                        resultData.config[0].couponType = resultData.config[0].dac_coupon_type;
                        resultData.config[0].couponId = resultData.config[0].dac_coupon_id;

                        if (resultData.config[0].couponType == 0 && resultData.config[0].couponId != 0) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_cash_coupon + "/cc_id/" + resultData.config[0].couponId,
                                    method: 'GET',

                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }
                                    resultData.config[0].couponName = data.data[0].cc_name;
                                    resultData.config[0].couponStartTime = data.data[0].cc_start_time;
                                    resultData.config[0].couponEndTime = data.data[0].cc_end_time;
                                    cb();
                                }
                            );
                        } else if (resultData.config[0].couponType == 1 && resultData.config[0].couponId != 0) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_course_coupon + "/cc_id/" + resultData.config[0].couponId,
                                    method: 'GET',

                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }
                                    resultData.config[0].couponName = data.data[0].cc_name == undefined ? 0 : data.data[0].cc_name;
                                    resultData.config[0].couponStartTime = data.data[0].cc_start_time == undefined ? 0 : data.data[0].cc_start_time;
                                    resultData.config[0].couponEndTime = data.data[0].cc_end_time == undefined ? 0 : data.data[0].cc_end_time;

                                    cb();
                                }
                            );
                        } else if (resultData.config[0].couponId != 0) {
                            console.log(123456789);
                            console.log(resultData.config[0].couponId);
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.searchExchangeCoupon + "/ec_id/" + resultData.config[0].couponId,
                                    method: 'GET',

                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }
                                    resultData.config[0].couponName = data.data[0].ec_name;
                                    resultData.config[0].couponStartTime = data.data[0].ec_start_time;
                                    resultData.config[0].couponEndTime = data.data[0].ec_end_time;
                                    cb();
                                }
                            );
                        } else {
                            resultData.config[0].couponName = '';
                            resultData.config[0].couponStartTime = '';
                            resultData.config[0].couponEndTime = '';
                            cb();
                        }
                    }
                );

            },
            //机构模板活动内容
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationDiscountsActivityContent + "/dac_oaid/" + restfulParams.oa_id + "/sort_order/dac_order-asc",
                        method: 'POST',
                        data: {
                            fields: [
                                "dac_type as type",
                                "dac_content as content"
                            ].join(",")
                        }
                    },

                    function (err, data) {
                        if (err) {
                            return cb(err, data);
                        }
                        resultData.content = data.data;
                        cb();
                    }
                )
            },
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}